package band.wukong.practice.webservice.kh.sample13.nongen;

import band.wukong.practice.webservice.kh.sample13.License;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.databinding.DataBinding;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.jaxb.JAXBDataBinding;
import org.apache.cxf.phase.Phase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;
import java.util.List;

/**
 * Object license interceptor - client - out
 *
 * @author wukong(wukonggg@139.com)
 */
public class ClientOutboundObjectLicenseInterceptor extends AbstractSoapInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(ClientOutboundObjectLicenseInterceptor.class);

    public ClientOutboundObjectLicenseInterceptor() {
        super(Phase.WRITE);

    }

    @Override
    public void handleMessage(SoapMessage msg) throws Fault {
        QName qName = new QName(CalcConst.SERVICE_NS, "license", "ns");
        DataBinding binding = null;
        try {
            binding = new JAXBDataBinding(License.class);
        } catch (JAXBException e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        Header header = new Header(qName, genLicense(), binding);

        List<Header> headers = msg.getHeaders();
        headers.add(header);
    }

    public License genLicense() {
        License license = new License();
        license.setUserName("Lemon Tree");
        license.setPassword("123456");
        return license;
    }
}
